﻿namespace WMS6.Web.Inbound
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CommonMethodServiceRef;
    using WMS6.Web.InboundServiceRef;

    public class ReceivingWeight : BasePage
    {
        protected Button btnSave;
        protected HtmlForm F4260;
        protected GridView gvReceivingWeight;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnGvCss;
        protected HtmlInputHidden hdnIndex;
        protected HtmlInputHidden hdnWeighted;
        protected HtmlHead Head1;
        protected Label lblASN;
        protected Label lblBarcode;
        protected Label lblExternalID;
        protected Label lblMsg;
        protected Label lblOwnerID;
        protected Label lblPoID;
        protected Label lblReceiptID;
        protected Label lblReceivingDate;
        protected Label lblSkuDescr;
        protected Label lblSkuPrice;
        protected Label lblSkuStyle;
        protected Label lblSkuWeight;
        protected Label lblVendorID;
        protected Label lblWeightedType;
        protected Label lblWeightType;
        protected HtmlLink mycss;
        protected Label one1;
        protected TextBox txtBarcode;
        protected TextBox txtExternalID;
        protected TextBox txtOwnerID;
        protected TextBox txtPoID;
        protected TextBox txtReceiptID;
        protected TextBox txtReceivingDate;
        protected TextBox txtSkuDescr;
        protected TextBox txtSkuPrice;
        protected TextBox txtSkuStyle;
        protected TextBox txtSkuWeight;
        protected TextBox txtVendorID;
        protected TextBox txtWeightedType;
        protected TextBox txtWeightType;

        protected void btnSave_Click(object sender, EventArgs e)
        {
            List<string> selectedID = new List<string>();
            List<string> weight = new List<string>();
            List<string> skuStyle = new List<string>();
            for (int i = 0; i < this.gvReceivingWeight.Rows.Count; i++)
            {
                if (((CheckBox) this.gvReceivingWeight.Rows[i].FindControl("chbSelect")).Checked && (this.gvReceivingWeight.DataKeys[i]["SkuID"] != null))
                {
                    selectedID.Add(this.gvReceivingWeight.DataKeys[i]["SkuID"].ToString());
                    weight.Add(this.gvReceivingWeight.DataKeys[i]["Weight"].ToString());
                    skuStyle.Add(this.gvReceivingWeight.DataKeys[i]["SkuStyle"].ToString());
                }
            }
            if (selectedID.Count == 0)
            {
                this.lblMsg.Text = "请至少选择一项!";
            }
            else
            {
                try
                {
                    this.lblMsg.Text = "更新" + InboundServiceClientProxy.GetInstance().UpdateSkuWeight(this.txtOwnerID.Text.Trim(), selectedID, weight, skuStyle) + "净重成功!";
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        protected void gvReceivingWeight_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                if (string.IsNullOrEmpty(((Label) e.Row.FindControl("lblSkuID")).Text) || (((Label) e.Row.FindControl("lblSkuID")).Text == "&nbsp;"))
                {
                    ((Image) e.Row.FindControl("StatusImg")).Visible = false;
                    ((Label) e.Row.FindControl("lblWeight")).Text = "";
                }
                if (!(string.IsNullOrEmpty(((Label) e.Row.FindControl("lblWeight")).Text) || (decimal.Parse(((Label) e.Row.FindControl("lblWeight")).Text) <= 0M)))
                {
                    ((Image) e.Row.FindControl("StatusImg")).ImageUrl = "~/Images/right.gif";
                }
                else
                {
                    ((Image) e.Row.FindControl("StatusImg")).ImageUrl = "~/Images/wrong.gif";
                }
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblItemNoDescr"));
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.ViewState["ReceivingWeight"] = new List<ReceivingWeightInfo>();
                base.BindData2GridView(this.gvReceivingWeight, this.SetGvidViewRow(new List<ReceivingWeightInfo>(), 15));
                this.txtReceiptID.Focus();
            }
        }

        private List<ReceivingWeightInfo> SetGvidViewRow(List<ReceivingWeightInfo> ReceivingWeightInfoList, int rowCount)
        {
            if (ReceivingWeightInfoList == null)
            {
                ReceivingWeightInfoList = new List<ReceivingWeightInfo>();
            }
            for (int i = ReceivingWeightInfoList.Count<ReceivingWeightInfo>(); i < rowCount; i++)
            {
                ReceivingWeightInfo receivingWeightInfo = new ReceivingWeightInfo();
                ReceivingWeightInfoList.Add(receivingWeightInfo);
            }
            return ReceivingWeightInfoList;
        }

        protected void txtBarcode_OnTextChanged(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!string.IsNullOrEmpty(this.txtReceiptID.Text.Trim()))
            {
                try
                {
                    if (!string.IsNullOrEmpty(this.txtBarcode.Text.Trim()) && !string.IsNullOrEmpty(this.txtSkuWeight.Text.Trim()))
                    {
                        this.txtSkuDescr.Text = "";
                        this.txtSkuPrice.Text = "";
                        this.txtSkuStyle.Text = "";
                        SkuInfo skuInfo = CommoneMethodServiceClientProxy.GetInstance().GetSkuByBarcode(this.txtBarcode.Text.Trim());
                        if (skuInfo != null)
                        {
                            List<ReceivingWeightInfo> ReceivingWeights = this.ViewState["ReceivingWeight"] as List<ReceivingWeightInfo>;
                            int flag = 0;
                            int flagWeighted = 0;
                            for (int i = 0; i < ReceivingWeights.Count; i++)
                            {
                                if ((ReceivingWeights[i].SkuID != null) && ((ReceivingWeights[i].SkuID.ToUpper() == skuInfo.SkuID.ToUpper()) && (skuInfo.OwnerID.ToUpper() == this.txtOwnerID.Text.ToUpper())))
                                {
                                    if ((ReceivingWeights[i].Weight == 0M) && (flagWeighted == 0))
                                    {
                                        this.hdnWeighted.Value = (int.Parse(this.hdnWeighted.Value) + 1).ToString();
                                        flagWeighted = 1;
                                    }
                                    ReceivingWeights[i].Weight = decimal.Parse(this.txtSkuWeight.Text.Trim());
                                    flag = 1;
                                }
                            }
                            if (flag == 0)
                            {
                                this.lblMsg.Text = "本订单没有此货品,请重新扫描条码!";
                                this.txtBarcode.Text = "";
                                this.txtBarcode.Focus();
                                return;
                            }
                            this.txtSkuDescr.Text = skuInfo.Descr;
                            this.txtSkuPrice.Text = base.GetPreferenceDigit(skuInfo.NetPrice);
                            this.txtSkuStyle.Text = skuInfo.SkuStyle;
                            this.txtWeightedType.Text = this.hdnWeighted.Value;
                            this.ViewState["ReceivingWeight"] = ReceivingWeights;
                            base.BindData2GridView(this.gvReceivingWeight, ReceivingWeights);
                        }
                        else
                        {
                            this.lblMsg.Text = "未找到匹配的货品请检查条码!";
                            this.txtBarcode.Text = "";
                            this.txtBarcode.Focus();
                            return;
                        }
                        this.txtSkuWeight.Text = "";
                        this.txtBarcode.Text = "";
                        this.txtBarcode.Focus();
                    }
                    else if (string.IsNullOrEmpty(this.txtBarcode.Text.Trim()))
                    {
                        this.txtBarcode.Focus();
                    }
                    else if (string.IsNullOrEmpty(this.txtSkuWeight.Text.Trim()))
                    {
                        this.txtSkuWeight.Focus();
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
            else
            {
                this.lblMsg.Text = "没有订单,请扫描入库单!";
                this.txtReceiptID.Focus();
            }
        }

        protected void txtReceiptID_OnTextChanged(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            this.txtVendorID.Text = "";
            this.txtOwnerID.Text = "";
            this.txtPoID.Text = "";
            this.txtExternalID.Text = "";
            this.txtWeightType.Text = "";
            this.txtReceivingDate.Text = "";
            ReceiptInfo receiptInfo = null;
            this.hdnWeighted.Value = "0";
            this.txtWeightedType.Text = this.hdnWeighted.Value;
            try
            {
                if (!string.IsNullOrEmpty(this.txtReceiptID.Text.Trim()))
                {
                    receiptInfo = InboundServiceClientProxy.GetInstance().GetReceiptByReceiptID(this.txtReceiptID.Text.Trim());
                    if (((receiptInfo != null) && (receiptInfo.ReceivingWeightInfos != null)) && (receiptInfo.ReceivingWeightInfos.Count > 0))
                    {
                        this.txtPoID.Text = receiptInfo.PoID;
                        this.txtExternalID.Text = receiptInfo.ExternalReceiptID;
                        this.txtWeightType.Text = receiptInfo.IsWeight;
                        this.txtReceivingDate.Text = base.GetPreferenceShortDate(receiptInfo.ReceiptDate);
                        this.txtOwnerID.Text = receiptInfo.OwnerID;
                        this.txtVendorID.Text = receiptInfo.VendorID;
                        this.ViewState["ReceivingWeight"] = receiptInfo.ReceivingWeightInfos;
                        base.BindData2GridView(this.gvReceivingWeight, this.SetGvidViewRow(receiptInfo.ReceivingWeightInfos, 15));
                        this.txtBarcode.Text = "";
                        this.txtBarcode.Focus();
                    }
                    else
                    {
                        this.lblMsg.Text = "未找到匹配的数据!";
                        this.txtReceiptID.Text = "";
                        this.txtReceiptID.Focus();
                        base.BindData2GridView(this.gvReceivingWeight, this.SetGvidViewRow(new List<ReceivingWeightInfo>(), 15));
                    }
                }
                else
                {
                    this.txtReceiptID.Text = "";
                    this.txtReceiptID.Focus();
                    base.BindData2GridView(this.gvReceivingWeight, this.SetGvidViewRow(new List<ReceivingWeightInfo>(), 15));
                }
            }
            catch (Exception ex)
            {
                this.txtReceiptID.Text = "";
                this.txtReceiptID.Focus();
                base.BindData2GridView(this.gvReceivingWeight, this.SetGvidViewRow(new List<ReceivingWeightInfo>(), 15));
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

